--- 模块功能：给台网中心发送心跳
-- @author zhfh
-- @module mqtt.mqttTask
-- @license MIT
-- @copyright openLuat
-- @release 2023.03.24
module(..., package.seeall)

require "misc"
require "mqtt"
require "mqttOutMsgQuakeRel"
--require "mqttOutMsg"
require "nvm"
-- 初始化nvm获取配置参数
nvm.init("config.lua", true)
-- 参数初始化
local clientId, longitude, latitude = nvm.get("info")[1], nvm.get("longitude"),
                                      nvm.get("latitude")
local clientType, msgType, hei, time = 2, 1, 0, 0
-- 字符串拼接
local function array_concat(...)
    local t = {}
    print(arg.n)
    for i = 1, arg.n do
        local array = arg[i]
        if type(array) == "table" then
            for j = 1, #array do t[#t + 1] = array[j] end
        else
            t[#t + 1] = array
        end
    end
    return t

end
-- floatTobyte
local function floatToByte(floatNum)
    local byteArr = {}
    local byteTable = {string.byte(string.format("%.3f", floatNum), 1, 4)}
    for i = 1, #byteTable do byteArr[i] = byteTable[i] end
    return byteArr
end
local strBytes = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
local endBytes = {0, 0, 0, 0, 0, 0, 0, 0}
-- string转bytes
local function strToBytes(str)
    local bytes = strBytes
    for i = 1, string.len(str) do
        bytes[i] = string.byte(string.sub(str, i, i))
    end
    return bytes
end
-- int或Long转Bytes
local function numToByteLow(num, numLength)
    local bytes = {}
    for i = 1, numLength do
        local byte = bit.band(num, 0xFF)
        table.insert(bytes, byte)
        num = bit.rshift(num, 8)
    end
    return bytes
end
local function bytesToISO8859String(bytes)

    local isoString = ""

    for i = 1, #bytes do

        local byte = bytes[i]

        local char = string.char(byte)

        -- 如果字节值大于127，则不是ISO-8859-1字符，需要进行错误处理

        if byte > 127 then error("Invalid ISO-8859-1 byte: " .. byte) end

        isoString = isoString .. char

    end

    return isoString

end
-- string转bytes
local function str2Bytes(str)
    local bytes = {}

    for i = 1, #str do

        local char = str:sub(i, i)

        local byte = string.byte(char)

        table.insert(bytes, byte)

    end
end
local function numToByteHigh(num, numLength)
    local bytes = {}
    for i = 1, numLength do
        -- local byte = bit.band(num, 0xFF)
        local intByte = bit.rshift(num, 8 * (numLength - i))
        local byte = bit.band(intByte, 0xFF)
        table.insert(bytes, byte)

    end
    return bytes
end

-- 启动MQTT客户端任务
local function heartBeatSend()
    local b = {}
    log.info("clientId:", clientId)
    log.info("clientType:", clientType)
    local lngInt, LatInt = math.floor(longitude * 100000),
                           math.floor(latitude * 100000)
    local clientByte = strToBytes(clientId)
    local clientTypeByte = numToByteHigh(clientType, 1)
    local msgTypeByte = numToByteHigh(msgType, 1)
    local longitudeByte = numToByteHigh(lngInt, 4)
    local latitudeByte = numToByteHigh(LatInt, 4)
    local heiByte = numToByteHigh(hei, 2)
    local timeByte = numToByteHigh(time, 4)
    b = array_concat(clientByte, clientTypeByte, msgTypeByte, longitudeByte,
                     latitudeByte, heiByte, timeByte, endBytes)
    print(table.concat(longitudeByte, ","))
    print(table.concat(latitudeByte, ","))
    print(table.concat(b, ","))
    local strSendMsg = string.char(unpack(b))
    log.info("strSendMsg:", strSendMsg)
    mqttOutMsgQuakeRel.insertQuakeRelMsg("EEW/USR", strSendMsg, 0)
    --mqttOutMsg.insertMsg("EEW/USR", strSendMsg, 0)
end
sys.timerLoopStart(heartBeatSend, 20000)
